package com.foreknow.view;


import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.User;
import com.sun.tools.javac.code.Type;

import java.sql.Connection;
import java.util.List;

/**
 * 测试Jdbc的工具类的用法
 */
public class Test {
  public static void main(String[] args) {
    //1.连接数据库
    DBManager dbManager = DBManager.getInstance();
    Connection conn = dbManager.getConnection();
    //2.对数据库进行操作
    //2.1 先要获取到JdbcTempleate类的对象
    JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
    jdbcTemplate.setConnection(conn);//将DBManager中获取到的conn传递给JdbcTemplate中的con
    //2.2 调用update方法完成添加操作  Object...values（这是一个动态可变的参数：有参数就传，没有就不传） 相当于  Object[] values
//    int isRight = jdbcTemplate.update("insert into user(username,password) values(?,?)","abc","123456");
//    System.out.println(isRight);

    //2.3对数据库进行查询操作
    MappingFactory mappingFactory = MappingFactory.getInstance();//获取到工厂类的对象
    EntityMapping mapping = mappingFactory.getMap(MappingFactory.USER_MAPPING); //UserMapping对象
    List<Object> list = jdbcTemplate.query("select * from user",mapping);
    for(int i = 0;i<list.size();i++){
      User user = (User) list.get(i);
      System.out.println(user.getId()+"--"+user.getUsername()+"--"+user.getPassword());
    }

  }
}
